package nc.ui.pub.filesystem;

import java.awt.Color;
import java.awt.Component;
import java.awt.Graphics;

import javax.swing.Action;
import javax.swing.Icon;
import javax.swing.JButton;
import javax.swing.border.EmptyBorder;

public class ToolBarButton extends JButton {
	private static final long serialVersionUID = 34938265883289327L;
	private static class RolloverIcon implements Icon{
		private Icon icon=null;
		private static Color color1= new Color(202,227,239);
		private static Color color2= new Color(54,179,243);
		public RolloverIcon(Icon icon) {
			super();
			this.icon = icon;
		}

		public int getIconHeight() {
			return icon==null?0:icon.getIconHeight();
		}

		public int getIconWidth() {
			return icon==null?0:icon.getIconHeight();
		}

		public void paintIcon(Component c, Graphics g, int x, int y) {
			if(icon != null){
				int dx = 2, dy = 2;
				g.setColor(color1);
				g.fillRoundRect(x-dx, y-dy, getIconWidth(), getIconHeight(), 2, 2);
				icon.paintIcon(c, g, x-dx, y-dy);
				g.setColor(color2);
				g.drawRoundRect(x-dx, y-dy, getIconWidth(), getIconHeight(), 2, 2);
			}
			
		}
		
	}
	public ToolBarButton(Action a) {
		super(a);
		initialized();
	}

	private void initialized() {
		Action a = getAction();
		String text = a != null ? (String) a.getValue(Action.NAME) : null;
		Icon icon = a != null ? (Icon) a.getValue(Action.SMALL_ICON) : null;
		boolean enabled = a != null ? a.isEnabled() : true;
		String tooltip = a != null ? (String) a.getValue(Action.SHORT_DESCRIPTION) : null;
		setText(text);
		setIcon(icon);
		setRolloverEnabled(true);
		setRolloverIcon(new RolloverIcon(icon));
		if (icon != null) {
			putClientProperty("hideActionText", Boolean.TRUE);
			setText("");
		}
		setHorizontalTextPosition(JButton.CENTER);
		setVerticalTextPosition(JButton.BOTTOM);
		setEnabled(enabled);
		setOpaque(false);
		setToolTipText(tooltip);
		setFocusPainted(false);
		setBorder(new EmptyBorder(2,2,2,2));
		setContentAreaFilled(false);
	}


}
